package Views;

import Controllers.ItemCTR;
import Models.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;

/**
 * @author Thomas, Morten og Erik
 */
public class ItemInfoView extends javax.swing.JFrame implements WindowListener
{
	private Item item;
	private ItemCTR controller = new ItemCTR();
	private Supplier supplierTemp = null;
	private SupplierPicker sp = null;

    public ItemInfoView(Item item)
	{
		this.item = item;
        initComponents();
		nameTF.setText(this.item.getName());
		manufacturerTF.setText(this.item.getManufacturer());
		supplierTF.setText(this.item.getSupplier().getName());
		priceSP.setValue(this.item.getPrice());
		if(this.item.getLocation() == Location.LumberYard)
		{ lumberYardRB.setSelected(true); }
		else
		{ hardwareStoreRB.setSelected(true); }
		minStockSP.setValue(this.item.getMinimumStock());
		maxStockSP.setValue(this.item.getMaximumStock());
		if(!item.isActivated())
		{ activatedCB.setSelected(false); }
    }

	ItemInfoView(Kitchen tableTopTemp) {
		throw new UnsupportedOperationException("Not yet implemented");
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        descriptionLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        manufacturerLabel = new javax.swing.JLabel();
        manufacturerTF = new javax.swing.JTextField();
        supplierLabel = new javax.swing.JLabel();
        supplierTF = new javax.swing.JTextField();
        findSupplierButton = new javax.swing.JButton();
        showSupplierButton = new javax.swing.JButton();
        priceLabel = new javax.swing.JLabel();
        priceSP = new javax.swing.JSpinner();
        locationLabel = new javax.swing.JLabel();
        lumberYardRB = new javax.swing.JRadioButton();
        hardwareStoreRB = new javax.swing.JRadioButton();
        stockLabel = new javax.swing.JLabel();
        minStockLabel = new javax.swing.JLabel();
        minStockSP = new javax.swing.JSpinner();
        maxStockLabel = new javax.swing.JLabel();
        maxStockSP = new javax.swing.JSpinner();
        editButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vareinformation");
        setLocationByPlatform(true);
        setResizable(false);

        descriptionLabel.setText("Her ses information om den valgte vare");

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        nameLabel.setText("Navn:");

        nameTF.setEnabled(false);

        manufacturerLabel.setText("Producent:");

        manufacturerTF.setEnabled(false);

        supplierLabel.setText("Leverandør: ");

        supplierTF.setEnabled(false);

        findSupplierButton.setText("Find");
        findSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findSupplierButtonActionPerformed(evt);
            }
        });

        showSupplierButton.setText("Info");
        showSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showSupplierButtonActionPerformed(evt);
            }
        });

        priceLabel.setText("Pris:");

        priceSP.setEnabled(false);

        locationLabel.setText("Afdeling:");

        lumberYardRB.setText("Trælasthandel");
        lumberYardRB.setEnabled(false);

        hardwareStoreRB.setText("Byggemarked");
        hardwareStoreRB.setEnabled(false);

        stockLabel.setText("Lager:");

        minStockLabel.setText("Min.");

        maxStockLabel.setText("Max.");

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Slet");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(descriptionLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 161, Short.MAX_VALUE)
                        .add(activatedCB))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(manufacturerLabel)
                            .add(nameLabel)
                            .add(supplierLabel)
                            .add(priceLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, nameTF)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, manufacturerTF)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(supplierTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 143, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findSupplierButton)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(showSupplierButton))))
                    .add(layout.createSequentialGroup()
                        .add(minStockLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(minStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(maxStockLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(maxStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(locationLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lumberYardRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(hardwareStoreRB))
                    .add(stockLabel)
                    .add(layout.createSequentialGroup()
                        .add(editButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 152, Short.MAX_VALUE)
                        .add(closeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(descriptionLabel)
                    .add(activatedCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(nameLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(manufacturerLabel)
                    .add(manufacturerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(12, 12, 12)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(supplierLabel)
                    .add(supplierTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findSupplierButton)
                    .add(showSupplierButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(priceLabel)
                    .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(locationLabel)
                    .add(lumberYardRB)
                    .add(hardwareStoreRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(stockLabel)
                .add(2, 2, 2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(minStockLabel)
                    .add(minStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(maxStockLabel)
                    .add(maxStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(removeButton)
                    .add(closeButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger")) {
			nameTF.setEnabled(true);
			manufacturerTF.setEnabled(true);
			supplierTF.setEnabled(true);
			priceSP.setEnabled(true);
			lumberYardRB.setEnabled(true);
			hardwareStoreRB.setEnabled(true);
			minStockSP.setEnabled(true);
			maxStockSP.setEnabled(true);
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		} else if (editButton.getText().equals("Gem")) {
			Location location = null;
			if(lumberYardRB.isSelected()) {
				location = Location.LumberYard;
			} else {
				location = Location.HardwareStore;
			}
			this.item.setName(nameTF.getText());
			this.item.setManufacturer(manufacturerTF.getText());
			this.item.setSupplier(this.supplierTemp);
			this.item.setPrice(Integer.parseInt(priceSP.getValue().toString()));
			this.item.setLocation(location);
			this.item.setMinimumStock(Integer.parseInt(minStockSP.getValue().toString()));
			this.item.setMaximumStock(Integer.parseInt(maxStockSP.getValue().toString()));
			nameTF.setEnabled(false);
			manufacturerTF.setEnabled(false);
			supplierTF.setEnabled(false);
			priceSP.setEnabled(false);
			lumberYardRB.setEnabled(false);
			hardwareStoreRB.setEnabled(false);
			minStockSP.setEnabled(false);
			maxStockSP.setEnabled(false);
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
}//GEN-LAST:event_editButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil slette "+item.getName()+"?", "Bekræftelse af sletning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.removeItem(item);
			this.dispose();
		}
}//GEN-LAST:event_removeButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
}//GEN-LAST:event_closeButtonActionPerformed

	private void findSupplierButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findSupplierButtonActionPerformed
	{//GEN-HEADEREND:event_findSupplierButtonActionPerformed
		sp = new SupplierPicker();
		sp.addWindowListener(this);
		sp.setVisible(true);
	}//GEN-LAST:event_findSupplierButtonActionPerformed

	private void showSupplierButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showSupplierButtonActionPerformed
	{//GEN-HEADEREND:event_showSupplierButtonActionPerformed
		new SupplierInfoView(this.supplierTemp);
	}//GEN-LAST:event_showSupplierButtonActionPerformed

	public void windowClosing(WindowEvent e)
	{
		if(sp != null)
		{
			this.supplierTemp = sp.getChosenSupplier();
			if (this.supplierTemp != null)
			{ supplierTF.setText(this.supplierTemp.getName()); }
			sp = null;
		}
	}

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JButton findSupplierButton;
    private javax.swing.JRadioButton hardwareStoreRB;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JRadioButton lumberYardRB;
    private javax.swing.JLabel manufacturerLabel;
    private javax.swing.JTextField manufacturerTF;
    private javax.swing.JLabel maxStockLabel;
    private javax.swing.JSpinner maxStockSP;
    private javax.swing.JLabel minStockLabel;
    private javax.swing.JSpinner minStockSP;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JSpinner priceSP;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton showSupplierButton;
    private javax.swing.JLabel stockLabel;
    private javax.swing.JLabel supplierLabel;
    private javax.swing.JTextField supplierTF;
    // End of variables declaration//GEN-END:variables
}
